<div class="row-fluid">
    <div id="content" class="row-fluid">
        <div class="box span12">
            <div class="box-header well">
                <h2>Quản lý thể loại sách</h2>
            </div>
            
            <div class="box-content">
                <div class="ajax-loaders" style="display: none;"></div>
                <div class="buttons fr">
                    <a href="#dialog-booktype" class="btn btn-danger" data-toggle="modal"><i class="icon icon-add icon-white"></i> Thêm thể loại</a>
                </div>
                <br />
                <hr class="cb" />
                <div class="table-content">
                    
                </div>
            </div>
        </div>
    </div>	
</div>

<!-- Form insert booktype -->
<div id="dialog-booktype" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Thêm Thể Loại Sách</h3>
  </div>
  <div class="modal-body">
    <form class="form-horizontal row-fluid" onsubmit="return insert_booktype(this)" name="form_insert_booktype" method="post" action="<?php echo $this->url(array('controller'=>'book','action'=>'index'))?>" >
        <div class="span6">
            <div class="control-group">
                <div class="control-label">Tên Thể Loại</div>
                <div class="controls">
                    <input class="input-xlarge required" type="text" name="TenTheLoai" value="" />
                </div>
            </div>
        </div>
    </form>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Thoát</a>
    <a href="#" class="btn btn-primary" onclick="return insert_booktype(form_insert_booktype)">Thêm</a>
  </div>
</div>

<!-- Form edit booktype -->
<div id="dialog-editbooktype" class="modal hide fade">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Cập Nhật Thể Loại Sách</h3>
  </div>
  <div class="modal-body">
    <form class="form-horizontal row-fluid" onsubmit="return edit_booktype(this)" name="form_edit_booktype" method="post" action="<?php echo $this->url(array('controller'=>'book','action'=>'index'))?>" >
        <div class="span6">
            <div class="control-group">
                <div class="control-label">Tên Thể Loại</div>
                <div class="controls">
                    <input type="hidden" name="MaTheLoaiSach" value="" />
                    <input class="input-xlarge required" type="text" name="TenTheLoai" value="" />
                </div>
            </div>
        </div>
    </form>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Thoát</a>
    <a href="#" class="btn btn-primary" onclick="return edit_booktype(form_edit_booktype)">Cập Nhập</a>
  </div>
</div>

<script>
$(function(){
    // Load du lieu
    $(".table-content").loadDataTable("<?php echo $this->url(array('controller'=>'booktype','action'=>'list'), null, true);?>");
    
});

// Them
function insert_booktype(form){
    if(checkRequired(form)){
        $(".ajax-loaders").show();
        var link = "<?php echo $this->url(array('controller'=>'booktype','action'=>'index'), null, true);?>";
         $.post(link,{ ajax_action:"insertBooktype", sdata: $(form).serialize() },function(rdata){
            $(".table-content").loadDataTable("<?php echo $this->url(array('controller'=>'booktype','action'=>'list'), null, true);?>");
            $(".ajax-loaders").hide();
            alert("Đã thêm thể loại mới");
            $("#dialog-booktype").modal("hide");
        });
    }
    return false;
}

// Xoa
function delete_booktype(id){
    if(confirm("Bạn có chắc muốn xóa thể loại này không?")) {
        $(".ajax-loaders").show();
        var link = "<?php echo $this->url(array('controller'=>'booktype','action'=>'index'), null, true);?>";
        $.get(link ,{ajax_action:"deleteBookType", sdata: id },function(){
            $(".table-content").loadDataTable("<?php echo $this->url(array('controller'=>'booktype','action'=>'list'), null, true);?>");
            $(".ajax-loaders").hide();
        },'json');
    }
    return false;
}

// Edit book
function open_modal_editbooktype(id_booktype){
    var link = "<?php echo $this->url(array('controller'=>'booktype','action'=>'index'), null, true);?>";
    // Load data
    $(".ajax-loaders").show();
    $('form[name="form_edit_booktype"]').find("input[name='MaTheLoaiSach']").val(id_booktype);
    $.get(link, {ajax_action: 'getBookType',sdata:id_booktype},function(rdata){
        $.each(rdata, function(name,value){
            $('form[name="form_edit_booktype"]').find("[name='"+name+"']").val(value);
        });        
        $("#dialog-editbooktype").modal("show");
        $(".ajax-loaders").hide();
    },'json');
    return false;
}

function edit_booktype(form){
    if(checkRequired(form)){
        $(".ajax-loaders").show();
        $("#dialog-editbooktype").find(".alert").hide();
        var link = "<?php echo $this->url(array('controller'=>'booktype','action'=>'index'), null, true);?>";
         $.post(link,{ ajax_action:"editBookType", sdata: $(form).serialize() },function(rdata){
            alert("Cập nhật thể loại sách thành công.");
            $("#dialog-editbooktype").modal("hide");
            $(".table-content").loadDataTable("<?php echo $this->url(array('controller'=>'booktype','action'=>'list'), null, true);?>");
            $(".ajax-loaders").hide();
        });
    }else{
        $("#dialog-insertbook").find(".alert").addClass("alert-error").show();
    }
    return false;
}

</script>